package com.knowif.util;

import com.github.pagehelper.PageHelper;
import com.knowif.search.Condition;
import com.knowif.search.SearchCondition;
import com.knowif.util.commonUtil.ReflexUtil;
import com.knowif.util.commonUtil.StringUtil;

import java.lang.reflect.Method;
import java.util.Date;

/**
 * @author ：Xh
 * @date ：Created in 2019/5/3 0003 15:00
 */
public class ExampleUtil {

    /**
     * 构造Example对象
     * @param <T>
     * @param obj
     * @param searchCondition
     * @return
     * @throws Exception
     */
    @SuppressWarnings("unchecked")
	public static <T> T buildExample(Object obj, SearchCondition searchCondition) throws Exception{
    	if(searchCondition == null){
    		return (T) obj;
    	}
        Method[] exampleMethods = obj.getClass().getMethods();
        //反射出Example里面的Criteria对象
        Method createCriteria = ReflexUtil.getMethod("createCriteria", obj, exampleMethods);
        Object criteria = createCriteria.invoke(obj);
        if(searchCondition.getSort() != null && ! StringUtil.isEmpty(searchCondition.getSort().getField())){
            String methodName = "setOrderByClause";
            Method method = ReflexUtil.getMethod(methodName, obj, exampleMethods);
            method.invoke(obj, (String)(searchCondition.getSort().getField() +" " + searchCondition.getSort().getStyle()));
        }
        if (searchCondition.getConditionList() != null && !searchCondition.getConditionList().isEmpty()) {
            for (Condition condition : searchCondition.getConditionList()) {
	                Method[] criteriaMethods = criteria.getClass().getMethods();
	                if (condition.getOperator() == 1) {
	                    String key = StringUtil.tranFirstToBig(condition.getCode());
	                    String methodName = "and" + key + "EqualTo";
	                    methodInvoke(condition, methodName, criteria, criteriaMethods);
	                }
	                if (condition.getOperator() == 2) {
	                    String key = StringUtil.tranFirstToBig(condition.getCode());
	                    String methodName = "and" + key + "GreaterThan";
	                    methodInvoke(condition, methodName, criteria, criteriaMethods);
	                }
	                if (condition.getOperator() == 3) {
	                    String key = StringUtil.tranFirstToBig(condition.getCode());
	                    String methodName = "and" + key + "GreaterThanOrEqualTo";
	                    methodInvoke(condition, methodName, criteria, criteriaMethods);
	                }
	                if (condition.getOperator() == 4) {
	                    String key = StringUtil.tranFirstToBig(condition.getCode());
	                    String methodName = "and" + key + "LessThan";
	                    methodInvoke(condition, methodName, criteria, criteriaMethods);
	                }
	                if (condition.getOperator() == 5) {
	                    String key = StringUtil.tranFirstToBig(condition.getCode());
	                    String methodName = "and" + key + "LessThanOrEqualTo";
	                    methodInvoke(condition, methodName, criteria, criteriaMethods);
	                }
	                if (condition.getOperator() == 6) {
	                    String key = StringUtil.tranFirstToBig(condition.getCode());
	                    String methodName = "and" + key + "NotEqualTo";
	                    methodInvoke(condition, methodName, criteria, criteriaMethods);
	                }
	                if (condition.getOperator() == 7) {
	                    String key = StringUtil.tranFirstToBig(condition.getCode());
	                    String methodName = "and" + key + "Like";
	                    methodInvoke(condition, methodName, criteria, criteriaMethods);
	                }
	                if (condition.getOperator() == 8) {
	                    String key = StringUtil.tranFirstToBig(condition.getCode());
	                    String methodName = "and" + key + "NotLike";
	                    methodInvoke(condition, methodName, criteria, criteriaMethods);
	                }
	                if (condition.getOperator() == 9) {
	                    String key = StringUtil.tranFirstToBig(condition.getCode());
	                    String methodName = "and" + key + "In";
	                    methodInvoke(condition, methodName, criteria, criteriaMethods);
	                }
	                if (condition.getOperator() == 10) {
	                    String key = StringUtil.tranFirstToBig(condition.getCode());
	                    String methodName = "and" + key + "NotIn";
	                    methodInvoke(condition, methodName, criteria, criteriaMethods);
	                }
//	            }
            }
        }
        if(searchCondition.getPageNumber() != 0 && searchCondition.getPageSize() != 0){
            //设置分页
            PageHelper.startPage(searchCondition.getPageNumber(), searchCondition.getPageSize());
        }
        return (T) obj;
    }

    public static void methodInvoke(Condition condition, String methodName, Object obj, Method[] methods) throws Exception{
        if ("Number".equalsIgnoreCase(condition.getType())) {
            Method method = ReflexUtil.getMethod(methodName, obj, methods);
            if(method != null){
            	method.invoke(obj, (Integer)condition.getValue());
            }
        }
        if ("String".equalsIgnoreCase(condition.getType())) {
            if(condition.getOperator() == 7){
                String value = condition.getValue().toString();
                condition.setValue("%" + value + "%");
            }
            Method method = ReflexUtil.getMethod(methodName, obj, methods);
            if(method != null){
            	method.invoke(obj, (String)condition.getValue());
            }
        }
        if ("Timestamp".equalsIgnoreCase(condition.getType())) {
            Method method = ReflexUtil.getMethod(methodName, obj, methods);
            if(method != null){
            	method.invoke(obj, (Date)condition.getValue());
            }
        }
        
    }

}